package com.example.flutter_meiqia;

import android.app.Activity;
import android.content.Intent;

import androidx.annotation.NonNull;

import com.meiqia.core.MQManager;
import com.meiqia.core.callback.OnInitCallback;
import com.meiqia.meiqiasdk.util.MQConfig;
import com.meiqia.meiqiasdk.util.MQIntentBuilder;

import java.util.HashMap;
import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** FlutterMeiqiaPlugin */
public class FlutterMeiqiaPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private Activity context;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_meiqia");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    String method = call.method;
    if (method.equals("initMQ")) {
      initMQ(call,result);
    } else if(method.equals("pushToMQVC")){
      pushToMeiqiaVC(call,result);
    }else {
      result.notImplemented();
    }
  }
  public void initMQ(@NonNull MethodCall call, @NonNull final Result result) {
    String appKey = call.argument("appKey");
    MQConfig.init(context, appKey, new OnInitCallback() {
      @Override
      public void onSuccess(String clientId) {
        result.success(true);
      }
      @Override
      public void onFailure(int code, String message) {
        result.success(false);
      }
    });

  }
  public void pushToMeiqiaVC(@NonNull MethodCall call, @NonNull final Result result) {

    String userId = call.argument("userId");
    String userName = call.argument("userName");
    String avatar = call.argument("avatar");
    MQIntentBuilder builder = new MQIntentBuilder(context);
    HashMap<String, String> clientInfo = new HashMap<String, String>();
    clientInfo.put("userId",userId);
    clientInfo.put("userName",userName);
    clientInfo.put("avatar",avatar);
//    builder.setClientInfo(clientInfo);
    builder.setClientInfo(clientInfo);
    builder.updateClientInfo(clientInfo);
    Intent intent = builder.build();

     context.startActivity(intent);
    result.success(true);
  }


//  func pushToMeiqiaVC(@NonNull MethodCall call, @NonNull Result result) {
//    //跳转聊天界面
//    let vc = MQChatViewManager.init()
//    vc.pushMQChatViewController(in: navigationController())
//    result(true);
//  }
//  func getLocalUnreadeMessages(@NonNull MethodCall call, @NonNull Result result) {
//    //获取未读消息数量
//
//    if let array =  MQManager.getLocalUnreadeMessages(){
//      result(array.count);
//    }else{
//      result(0)
//    }
//
//  }
  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    this.context = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {

  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

  }

  @Override
  public void onDetachedFromActivity() {

  }
}
